#include "encoder_task.h"
#include "oled_task.h"
#include "ble_send_task.h"
#include "ble_receive_task.h"

#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"

#include "ohos_init.h"
#include "cmsis_os2.h"

#include <stdio.h>

#define TIMEOUT_MAX   40

#ifndef RESET
#define RESET 0
#define SET   1
#endif


encoder_task_data_t encoder_task_data;
extern show_data_t show_data;
extern ble_receive_data_t ble_receive_data;

static void encoder_isr_function0(char *arg);
static void encoder_isr_function1(char *arg);

static void change_show_num(void);
static void encoder_down(void);
static void enter_to_select_function(void);
static void enter_to_function_set(void);
static void enter_to_wifi_function(void);
static void enter_to_server_function(void);
static void enter_to_ble_function(void);
static void enter_to_ctrl_function(void);
static void enter_to_setting_function(void);


static void encoder_left(void);
static void change_select_num(void);
static void change_wifi_function(void);
static void change_server_function(void);
static void change_ble_function(void);
static void change_ctrl_function(void);
static void change_setting_function(void);
	
static void encoder_right(void);

static void encoder_gpio_init(void)
{
	GpioInit();
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_IO_FUNC_GPIO_0_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_GPIO_DIR_IN);
    IoSetPull(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_IO_PULL_UP);
	GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, encoder_isr_function0, NULL);
   
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_IO_FUNC_GPIO_1_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_GPIO_DIR_IN);
    IoSetPull(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_IO_PULL_UP);
	GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, encoder_isr_function1, NULL);

    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_GPIO_DIR_IN);
    IoSetPull(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_PULL_UP);
}


void encoder_task(void* parameter)
{
	(void) parameter;
	encoder_gpio_init();
	encoder_task_data.timeout_left_flag = RESET;
	encoder_task_data.timeout_right_flag = RESET;
	while(1)
	{
		// get_encoder_state();
		change_show_num();
		
		osDelay(10);
	}
}

void encoder_task_creat(void)
{
    osThreadAttr_t attr;

    attr.name       = "encoder_task";
    attr.attr_bits  = 0U;
    attr.cb_mem     = NULL;
    attr.cb_size    = 0U;
    attr.stack_mem  = NULL;
    attr.stack_size = 10240;
    attr.priority   = osPriorityNormal2;

    if (osThreadNew(encoder_task, NULL, &attr) == NULL)
    {
        printf("[encoder_task_creat] Falied to create encoder_task!\n");
    }
}

APP_FEATURE_INIT(encoder_task_creat);



//按下功能键
static void encoder_isr_function0(char *arg)
{
    (void) arg;
    int time_out = 0;
    WifiIotGpioValue gpio_value;
    osDelay(1); //延时10ms
    GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_0, &gpio_value);
    if(gpio_value == WIFI_IOT_GPIO_VALUE0)
    {
        while(gpio_value == WIFI_IOT_GPIO_VALUE0 && time_out <= TIMEOUT_MAX)    //等待按键释放或者超时
        {
            GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_0, &gpio_value);
            time_out++;
            osDelay(1); //延时10ms
        }
		encoder_task_data.buzzer_state = 1;
		encoder_task_data.encoder_state = ENCODER_DOWN;
    }
}


static void encoder_isr_function1(char *arg)
{
    (void) arg;
    int time_out = 0;
    osDelay(1); //延时10ms
    WifiIotGpioValue gpio_value_a;
    WifiIotGpioValue gpio_value_b;
    GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_1, &gpio_value_a);
    GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_2, &gpio_value_b);

    if(gpio_value_a == WIFI_IOT_GPIO_VALUE0 && gpio_value_b == WIFI_IOT_GPIO_VALUE1) //A脚高电平时，B脚为高电平，顺时针旋转；A脚先变为低电平，此时B脚还为高电平。
    {
        while(gpio_value_a == WIFI_IOT_GPIO_VALUE0 && time_out <= TIMEOUT_MAX)     //超时时间为3s
        {
            GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_1, &gpio_value_a);
            time_out++;
            osDelay(1); //延时10ms
        }
		encoder_task_data.buzzer_state = 1;
		encoder_task_data.encoder_state = ENCODER_RIGHT;
    }
    else if(gpio_value_a == WIFI_IOT_GPIO_VALUE0 && gpio_value_b == WIFI_IOT_GPIO_VALUE0)    //A脚低电平时，B脚为高电平，逆时针旋转；B脚先变为低电平，此时AA脚还为高电平。
    {
        while(gpio_value_a == WIFI_IOT_GPIO_VALUE0 && time_out <= 300)
        {
            GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_1, &gpio_value_a);
            time_out++;
            osDelay(1); //延时10ms
        }
		encoder_task_data.buzzer_state = 1;
		encoder_task_data.encoder_state = ENCODER_LEFT;
    }
}


#if 0
static void get_encoder_state(void)
{
	WifiIotGpioValue gpio_value_a = 0;
	WifiIotGpioValue gpio_value_b = 0;
	uint16_t time_out = 0;
    WifiIotGpioValue gpio_value = 0;
	GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_0, &gpio_value);

	if(gpio_value == WIFI_IOT_GPIO_VALUE0)
	{
		osDelay(1);
        GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_0, &gpio_value);
		if(gpio_value == WIFI_IOT_GPIO_VALUE0)
		{
			time_out = 0;
			while(gpio_value == WIFI_IOT_GPIO_VALUE0 && time_out <= TIMEOUT_MAX)
			{
				time_out++;
				osDelay(1);
                GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_0, &gpio_value);
			}
			encoder_task_data.buzzer_state = 1;
			encoder_task_data.encoder_state = ENCODER_DOWN;
		}
	}
	
	GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_1, &gpio_value_a);
    GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_2, &gpio_value_b);
	if(gpio_value_a == RESET && gpio_value_b == SET) //顺时针
	{
		osDelay(1);
        GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_1, &gpio_value_a);
		if(gpio_value_a == RESET )
		{				
			time_out = 0;
			while(gpio_value_a == RESET && time_out < TIMEOUT_MAX)
			{
				time_out++;
				osDelay(1);
                GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_1, &gpio_value_a);
			}
			if(time_out == TIMEOUT_MAX)
			{
				encoder_task_data.timeout_left_flag = SET;
			}
			else
			{
				time_out = 0;
                GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_2, &gpio_value_b);
				while(gpio_value_b == RESET && time_out < TIMEOUT_MAX)
				{
					time_out++;
					osDelay(1);
                    GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_2, &gpio_value_b);
				}
			}
			if(time_out == TIMEOUT_MAX)
			{
				encoder_task_data.timeout_right_flag = SET;
			}
			else
			{
				encoder_task_data.timeout_right_flag = RESET;
				encoder_task_data.buzzer_state = 1;
				encoder_task_data.encoder_state = ENCODER_RIGHT;
			}
		}
	}
	else if(gpio_value_a == SET && gpio_value_b == RESET) //逆时针
	{
		osDelay(1);
        GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_2, &gpio_value_b);
		if(gpio_value_b == RESET)
		{
			time_out = 0;
			while(gpio_value_b == RESET && time_out < TIMEOUT_MAX)
			{
				time_out++;
				osDelay(1);
                GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_2, &gpio_value_b);
			}
			if(time_out == TIMEOUT_MAX)
			{
				encoder_task_data.timeout_left_flag = SET;
			}
			else
			{
				time_out = 0;
                GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_1, &gpio_value_a);
				while(gpio_value_a == RESET && time_out < TIMEOUT_MAX)
				{
					time_out++;
					osDelay(1);
                    GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_1, &gpio_value_a);
				}
			}
			if(time_out == TIMEOUT_MAX)
			{
				encoder_task_data.timeout_left_flag = SET;
			}
			else
			{
				encoder_task_data.buzzer_state = 1;
				encoder_task_data.timeout_left_flag = RESET;
				encoder_task_data.encoder_state = ENCODER_LEFT;
			}
		}
	}
}
#endif
/**********************************************************************************************************************************
                                                      根据按键状态改变显示内容
***********************************************************************************************************************************/
static void change_show_num(void)
{
	switch(encoder_task_data.encoder_state)
	{
		case ENCODER_DOWN  : encoder_down();break;
		case ENCODER_LEFT  : encoder_left();break;
		case ENCODER_RIGHT : encoder_right();break;
		case NOT_OPERATION : break;
		default            : break;
	}
	encoder_task_data.encoder_state = NOT_OPERATION;//按键数据处理完毕，将按键状态改为没有操作
}
/************************************************************************************************************************
                                                          按键按下
************************************************************************************************************************/
static void encoder_down(void)
{
	switch(show_data.show_num)
	{
		case SHOW_HOMEPAGE : enter_to_select_function(); break;
		case SHOW_SELECT   : enter_to_function_set(); break;
		case SHOW_WIFI     : enter_to_wifi_function(); break;    
        case SHOW_SERVER   : enter_to_server_function();break;
		case SHOW_BLE      : enter_to_ble_function(); break;
		case SHOW_CTRL     : enter_to_ctrl_function();break;
		case SHOW_SETTING  : enter_to_setting_function(); break;
		case SHOW_BACK     : break;
		default            : break;
	}
}
//运行选择功能
static void enter_to_select_function(void)
{
	show_data.show_num_last        = show_data.show_num;
	show_data.show_num             = SHOW_SELECT;    
	show_data.show_select_state    = SELECT;
	show_data.show_select_num      = SHOW_HOMEPAGE;
}
//运行选择的功能
static void enter_to_function_set(void)
{
	show_data.show_select_num_last = RESET;
	show_data.show_select_state	= RUN;
}

static void enter_to_wifi_function(void)
{
	show_data.show_wifi_state = RUN;
}
static void enter_to_server_function(void)
{
	show_data.show_server_state = RUN;
}

//运行选择的蓝牙功能
static void enter_to_ble_function(void)
{
	show_data.show_ble_state = RUN;
	// ble_data.ble_function_num = show_data.show_ble_num;
}

//运行选择的控制功能
static void enter_to_ctrl_function(void)
{
	if(show_data.show_select_slave_state == SELECT)
	{
		show_data.show_select_slave_state = RUN;
	}
	else
	{
		show_data.show_ctrl_state = RUN;
	}
}

//运行选择的设置功能
static void enter_to_setting_function(void)
{
	show_data.show_setting_state = RUN;
}

/************************************************************************************************************************
                                                             编码器左转
************************************************************************************************************************/
static void encoder_left(void)
{
	switch(show_data.show_num)
	{
		case SHOW_HOMEPAGE : break;
		case SHOW_SELECT   : change_select_num();    break;
		case SHOW_WIFI     : change_wifi_function(); break;    
        case SHOW_SERVER   : change_server_function();break;
		case SHOW_BLE      : change_ble_function();  break;
		case SHOW_CTRL     : change_ctrl_function(); break;
		case SHOW_SETTING  : change_setting_function(); break;
		default            : break;
	}
}

static void change_select_num(void)
{
	if(encoder_task_data.encoder_state == ENCODER_LEFT)
	{
		show_data.show_select_num--;
		if(show_data.show_select_num == SHOW_SELECT)
        {
            show_data.show_select_num--;
        }
        else if(show_data.show_select_num <= SHOW_MIN)
        {
            show_data.show_select_num = SHOW_BACK;
        }
	}
	else
	{
		show_data.show_select_num++;
        if(show_data.show_select_num == SHOW_SELECT)
        {
            show_data.show_select_num++;
        }
        else if(show_data.show_select_num >= SHOW_MAX)
        {
            show_data.show_select_num = SHOW_HOMEPAGE;
        }
	}
}

static void change_wifi_function(void)
{
	if(encoder_task_data.encoder_state == ENCODER_LEFT)
	{
		show_data.show_wifi_num--;
        if(show_data.show_wifi_num <= 0)
        {
            show_data.show_wifi_num = 3;
        }
	}
	else
	{
		show_data.show_wifi_num++;
        if(show_data.show_wifi_num >= 4)
        {
            show_data.show_wifi_num = 1;
        }
	}
}
static void change_server_function(void)
{
	if(encoder_task_data.encoder_state == ENCODER_LEFT)
	{
		show_data.show_server_num--;
        if(show_data.show_server_num <= 0)
        {
            show_data.show_server_num = 3;
        }
	}
	else
	{
		show_data.show_server_num++;
        if(show_data.show_server_num >= 4)
        {
            show_data.show_server_num = 1;
        }
	}
}

static void change_ble_function(void)
{
	if(encoder_task_data.encoder_state == ENCODER_LEFT)
	{
		show_data.show_ble_num--;
        if(show_data.show_ble_num <= BLE_CMD_MIN)
        {
            show_data.show_ble_num = 3;
        }
	}
	else
	{
		show_data.show_ble_num++;
        if(show_data.show_ble_num >= 4)
        {
            show_data.show_ble_num = BLE_RESTORE;
        }
	}
}

static void change_ctrl_function(void)
{
	if(show_data.show_select_slave_state == SELECT)//还在选择slave
	{
		if(encoder_task_data.encoder_state == ENCODER_LEFT)
		{
			show_data.show_select_slave_num--;
			if(show_data.show_select_slave_num <= 0)
			{
				show_data.show_select_slave_num = ble_receive_data.ble_num+1;
			}
		}
		else
		{
			show_data.show_select_slave_num++;
			if(show_data.show_select_slave_num >= ble_receive_data.ble_num+2)
			{
				show_data.show_select_slave_num = 1;
			}
		}
	}
	else if(show_data.show_ctrl_state == SELECT)
	{
		if(encoder_task_data.encoder_state == ENCODER_LEFT)
		{
			show_data.show_ctrl_num--;
			if(show_data.show_ctrl_num <= 0)
			{
				show_data.show_ctrl_num = 3;
			}
		}
		else
		{
			show_data.show_ctrl_num++;
			if(show_data.show_ctrl_num >= 4)
			{
				show_data.show_ctrl_num = 1;
			}
		}
	}
	
}

static void change_setting_function(void)
{
	if(encoder_task_data.encoder_state == ENCODER_LEFT)
	{
		show_data.show_setting_num--;
        if(show_data.show_setting_num <= 0)
        {
            show_data.show_setting_num = 3;
        }
	}
	else
	{
		show_data.show_setting_num++;
        if(show_data.show_setting_num >= 4)
        {
            show_data.show_setting_num = 1;
        }
	}
}


/************************************************************************************************************************
                                                        编码器右转
************************************************************************************************************************/
static void encoder_right(void)
{
	switch(show_data.show_num)
	{
		case SHOW_HOMEPAGE : break;
		case SHOW_SELECT   : change_select_num();    break;
		case SHOW_WIFI     : change_wifi_function(); break;    
        case SHOW_SERVER   : change_server_function();break;
		case SHOW_BLE      : change_ble_function();  break;
		case SHOW_CTRL     : change_ctrl_function(); break;
		case SHOW_SETTING  : change_setting_function(); break;
		default            : break;
	}
}

